# 书中要求所排序的数独立均匀的分布在 [0,1)区间上 P（112）
import math


def partition(A, p, r):
    x = A[r]
    i = p-1
    for j in range(p, r):
        if A[j] <= x:
            i = i+1
            A[i], A[j] = A[j], A[i]
    A[i+1], A[r] = A[r], A[i+1]
    return i+1


def quick_sort(A, p, r):
    if p < r:
        q = partition(A, p, r)
        quick_sort(A, p, q-1)
        quick_sort(A, q+1, r)


def bucket_sort(A):
    n = len(A)
    B = [[] for _ in range(n)]
    for i in range(n):
        B[math.floor(n*A[i])].append(A[i])
    for j in range(n):
        quick_sort(B[j],0,len(B[j])-1)

    A.clear()
    for x in B:
        for y in x:
            A.append(y)


A = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68]
bucket_sort(A)
print(A)

